package com.credithc.signature.job.service;

import com.credithc.signature.job.handler.CallbackHandler;
import com.credithc.signature.job.handler.GenerateFileHandler;
import com.credithc.signature.job.handler.ProcessSignHandler;
import com.credithc.signature.job.handler.UploadFileHandler;
import com.credithc.signature.job.ro.HandleRequest;
import com.credithc.signature.job.ro.JobRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉25⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Service
public class SignatureService {

    /**
     * 处理失败合同
     * 执行流程：
     * a、生成合同
     * b、执行签章
     * c、上传合同
     * d、回调业务
     *
     * @param jobRequest
     */
    public void process(JobRequest jobRequest) {
        HandleRequest handleRequest = HandleRequest.builder().jobRequest(jobRequest).build();
        GenerateFileHandler generateFileHandler = new GenerateFileHandler();
        ProcessSignHandler processSignHandler = new ProcessSignHandler();
        UploadFileHandler uploadFileHandler = new UploadFileHandler();
        CallbackHandler callbackHandler = new CallbackHandler();

        try {
            Thread.currentThread().setName("合同重试【消费者】-线程-" + Thread.currentThread().getId());
            // 执行任务
            generateFileHandler.setNextHandler(processSignHandler);
            processSignHandler.setNextHandler(uploadFileHandler);
            uploadFileHandler.setNextHandler(callbackHandler);
            generateFileHandler.handleRequest(handleRequest);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error("－消息－: requestSn={},执行合同重试失败,错误信息如下:{}\n", jobRequest.getRequestSn(), message);
        }
    }
}
